package com.hdoit.kylin.logical.group.dao.impl;
import java.util.List;

import org.springframework.stereotype.Component;

import com.hdoit.kylin.logical.group.dao.ITaskDao;
import com.hdoit.kylin.logical.group.model.Group;
import com.hdoit.kylin.logical.group.model.Task;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.viewmodel.TaskSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
* @author luozhicheng
* @email
* @createtime 2017年07月18日 20:37:50
* @comment
*/
@Component
public class TaskDaoImpl extends SuperDao<Task> implements ITaskDao{

	@Override
	public List<Task> queryListByGroupId(TaskSearch search,PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		if(search.getGroupId()!=null){
			finderExpression.select("task.*,group_info.groupName as theGroupName,student.studentName as theStudentName,teacher.teacherName as theTeacherName")
										 .from(Task.class,"task")
										 .leftJoin(Student.class,"student")
										 .on("task.employeeId","student.employeeId")
										  .leftJoin(Teacher.class,"teacher")
										 .on("task.publisherId","teacher.employeeId")
										 .leftJoin(Group.class,"group_info")
										 .on("task.groupId","group_info.groupId")
										 .andWhere("task.groupId=#{groupId}")
										 .setParam("groupId", search.getGroupId())
										 .setMainTableAlias("task");		
		}
		else{
			finderExpression.select("task.*,group_info.groupName as theGroupName,student.studentName as theStudentName,teacher.teacherName as theTeacherName")
										 .from(Task.class,"task")
										 .leftJoin(Student.class,"student")
										 .on("task.employeeId","student.employeeId")
										  .leftJoin(Teacher.class,"teacher")
										 .on("task.publisherId","teacher.employeeId")
										 .leftJoin(Group.class,"group_info")
										 .on("task.groupId","group_info.groupId")
										 .setMainTableAlias("task");		
		}
		return queryList(finderExpression,search,pagerModel);
	}

	@Override
	public int queryCountByGroupId(TaskSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		if(search.getGroupId()!=null){
			finderExpression.select("count(DISTINCT task.taskId) as count")
										 .from(Task.class,"task")
										 .leftJoin(Student.class,"student")
										 .on("task.employeeId","student.employeeId")
										 .leftJoin(Group.class,"group_info")
										 .on("task.groupId","group_info.groupId")
										 .andWhere("task.groupId=#{groupId}")
										 .setParam("groupId", search.getGroupId())
										 .setMainTableAlias("task");
		}
		else{
			finderExpression.select("count(DISTINCT task.taskId) as count")
										 .from(Task.class,"task")
										 .leftJoin(Student.class,"student")
										 .on("task.employeeId","student.employeeId")
										 .leftJoin(Group.class,"group_info")
										 .on("task.groupId","group_info.groupId")
										 .setMainTableAlias("task");		
		}
		return queryCount(finderExpression,search);	
		
	}
}